const md = require('../../utils/md5.js');
const app = getApp();
const post = require('../../utils/post.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var that =this;
    if(options.id !=''){
      that.setData({
        frid : options.id
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that =this;
    // 获取时间戳
    var apinameIsMeb = "/BO/BA_Friends/Query";
    var timestamp = new Date().getTime();
    timestamp = parseInt(timestamp / 1000);
    var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
    wx.request({
      //后台接口地址
      url: app.server.url + apinameIsMeb,
      data: {
        sign: md5IsMeb,
        appid: app.server.appid,
        tmsp: timestamp,
        where: 'RelID =' + that.data.frid
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        console.log(res);
        if(res.data.data.length >0){
          var star = res.data.data[0].FrStarLevel;
          var starnone =5-star;
          if (res.data.data[0].FrServiceArea &&　res.data.data[0].FrServiceArea.length > 12) {
            res.data.data[0].FrServiceArea = res.data.data[0].FrServiceArea.substring(0, 10) + "...";
          }  
          if (res.data.data[0].FrBrands && res.data.data[0].FrBrands.length > 12) {
            res.data.data[0].FrBrands = res.data.data[0].FrBrands.substring(0, 10) + "...";
          }
          that.setData({
            frinfo: res.data.data[0],
            star:star,
            starnone:starnone
          })
          // that.gethistory();
          // that.getshop(res.data.data[0].FrMebID);
        }
      }
    })
  },

  gethistory:function(){
    var that = this;
    var datas = {}; 
    var  url = "/BO/TR_Detail/Query";
    var where = "AccID = '" + that.data.frinfo.MyMebID + "' and MebID ='" + that.data.frinfo.FrMebID+"'";
   
    // 获取返利详情
    // console.log(time);
    datas.where = where;
    var datacall = function (res) {
      console.log(res);
      if (res.data.data.length != 0) {
        var moneyhistory=[];
        for (var i = 0; i < res.data.data.length;i++){
          moneyhistory[i]=res.data.data[i];
          moneyhistory[i].AccDate = moneyhistory[i].AccDate.split(' ')[0];
          moneyhistory[i].AccDate = moneyhistory[i].AccDate.replace("-", '.');
          moneyhistory[i].AccDate = moneyhistory[i].AccDate.replace("-", '.');
        }
        that.setData({
          moneyhistory: moneyhistory,
        })
      }
    }
    var res2 = post.getdata(url, datas, datacall);
  },
  call:function(){
    var that =this; 
    wx.makePhoneCall({
      phoneNumber: that.data.frinfo.FrTel,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  bememo:function(){
    wx.navigateTo({
      url: '/pages/memo/memo?id=' + this.data.frinfo.RelID + "&name=" + this.data.frinfo.FrNickName + "&status=" + this.data.frinfo.Fr_BOState + "&memoname=" + this.data.frinfo.MemoName + "&memo=" + this.data.frinfo.Memo,
    })
  },
  zhuanzhang:function(){
    var that =this;
    wx.navigateTo({
      url: '/pages/transfer/transfer?id=' + that.data.frinfo.FrMebID + "&imgurl=" + that.data.frinfo.FrPhotoUrl + "&mebname=" + that.data.frinfo.FrMebName + "&state=" + that.data.frinfo.Fr_BOState + "&fraccid=" + that.data.frinfo.DepID,
    })
  },
  tohistory:function(){
    var that =this;
    wx.navigateTo({
      url: '/pages/thistory/thistory?mebid=' + wx.getStorageSync('depid') + "&fmebid=" + that.data.frinfo.FrMebID + "&fraccid=" + that.data.frinfo.DepID,
    })
  },
  getshop:function(id){
    var that = this;
    var datas = {};
    var url = "/BO/AP_ShopApply/Query";
    var where = "MebID = "+id;
    datas.where = where;
    var datacall = function (res) {
      console.log(res);
      if (res.data.data.length != 0) {
        var urls = [];
        urls[0] = res.data.data[0].LicenceImg;
        urls[1] = res.data.data[0].ShopImgUrl0;
        urls[2] = res.data.data[0].ShopImgUrl1;
        urls[3] = res.data.data[0].ShopImgUrl3;
      }
      that.setData({
        urls:urls
      })
    }
    var res2 = post.getdata(url, datas, datacall);
  },
  showshops:function(){
    // wx.previewImage({
    //   current: '', // 当前显示图片的http链接
    //   urls: this.data.urls // 需要预览的图片http链接列表
    // })
    wx.navigateTo({
      url: '/pages/shopdetail/shopdetail?id=' + this.data.frinfo.DepID+"&type=show",
    })
  }
})